package com.ruoyi.chat.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.chat.domain.MoviesEntity;
import com.ruoyi.chat.dto.ChatDtoReq;
import com.ruoyi.chat.dto.MovieResp;
import com.ruoyi.chat.entity.ChatTypeEnum;
import com.ruoyi.chat.mapper.MovieQueryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
@Slf4j
public class ChatMovieService {
    @Resource
    MovieQueryMapper movieQueryMapper;
    public List<MovieResp> chat(ChatDtoReq  req){
        if(ChatTypeEnum.MOVIE.getCode().equals(req.getCategory())){
            log.info("想要下载的影视文件名：{}",req);
            QueryWrapper<MoviesEntity> moviesQuery = new QueryWrapper<>();
            moviesQuery.like("title",req.getMessage());
            List<MoviesEntity> moviesResult = movieQueryMapper.selectList(moviesQuery);
            List<MovieResp> movieResp = IntStream.range(0, moviesResult.size())
                    .mapToObj(i -> {
                        MoviesEntity movie = moviesResult.get(i);
                        MovieResp resp = new MovieResp();
                        resp.setId((long) (i + 1));
                        resp.setName(movie.getTitle());
                        resp.setUrl(movie.getMagnetLink());
                        return resp;
                    })
                    .collect(Collectors.toList());

            return movieResp;
        }
        return null;

    }
}
